<template>
  <view class="newchat">
    <!-- <u-navbar :autoBack="true" title="模型角色" bgColor="rgba(246, 247, 249, 1)"></u-navbar> -->
    <view class="title">请选择一个角色进行沉浸式对话</view>
    <view>
      <Swiper
        :list="list3"
        previousMargin="69"
        nextMargin="69"
        height="1054rpx"
        @changee="(e) => (current = e)"
        imgMode="heightFix"
        circular
        :autoplay="false"
        radius="6"
        bgColor="rgb(246, 247, 249)"
      ></Swiper>
    </view>
    <view class="btn">
      <u-button
        @click="btnClick"
        style="box-shadow: 0px 1px 8px rgba(10, 63, 255, 0.35); font-size: 32rpx"
        text="开始聊天"
        color="rgba(10, 63, 255, 1)"
      ></u-button>
      <u-checkbox-group @change="change">
        <u-checkbox
          v-model="checked"
          :checked="Rvalue === same"
          shape="circle"
          :name="same"
          label="设为默认角色"
          labelColor="rgba(163, 163, 163, 1)"
          size="28rpx"
        ></u-checkbox>
      </u-checkbox-group>
    </view>
  </view>
</template>
<script>
import Swiper from './Swiper'
import { chatModelList } from '@/network/api'
export default {
  options: {
    styleIsolation: 'shared',
  },
  components: {
    Swiper,
  },

  name: '',
  data() {
    return {
      checked: '',
      list3: [],
      item: {},
      current: 0,
      Rvalue: -1,
      same: 0,
    }
  },
  created() {
    this.getData()
    setTimeout(() => {
      this.Rvalue = this.list3.findIndex((item) => {
        return item.id == this.setting.default_model_id
      })
      console.log('this.Rvalue', this.Rvalue)
      console.log('this.setting', this.setting)
    }, 2000)
  },
  computed: {
    setting() {
      return this.$store.state.chatAI.settingUserInfo
    },
  },
  watch: {
    current: {
      handler(val) {
        // #ifdef H5
        this.item = this.list3[this.current]
        this.$store.commit('SETSYSTEMROLE', this.item)
        this.same = this.current
        // #endif
        // #ifdef MP-WEIXIN
        console.log('this.current', this.current)
        console.log('this.Rvalue两者什么时候相等', this.Rvalue)
        console.log('this.current.detail.__args__[0]', this.current.detail.__args__[0])
        this.same = this.current.detail.__args__[0]
        this.item = this.list3[this.current.detail.__args__[0]]
        this.$store.commit('SETSYSTEMROLE', this.item)
        // #endif
      },
      deep: true,
    },
  },
  methods: {
    async change(e) {
      this.Rvalue = e[0]
      console.log(this.Rvalue)
      console.log(this.item.id)
      await this.$store.dispatch('IuserSettingInfoUpdate', {
        default_model_id: e.length > 0 ? this.item.id : -1,
      })
      this.$store.dispatch('IuserSettingInfo')
    },
    async getData() {
      try {
        const res = await this.$store.dispatch('IchatModelList')
        console.log(res)
        this.list3 = res
        this.item = res[0]
        this.$store.commit('SETSYSTEMROLE', this.item)
      } catch (error) {
        console.log(error)
        uni.showToast({
          title: 'error',
          icon: 'error',
        })
      }
    },
    async btnClick() {
      setTimeout(async () => {
        const chat = {
          id: uni.$u.guid(),
          // #ifdef H5
          id: 666,
          // #endif
          title: '新的聊天',
          desc: this.item.desc,
          time: new Date().getTime(),
          generatedTitle: false,
          length: 0,
          modelId: this.item.id,
          modelName: this.item.name,
          avatar: this.item.avatar_url,
        }
        this.$store.commit('CLEARLIST')
        // 这里就要开始新建会话了
        let temp = await this.$store.dispatch('IconversationNew', {
          name: chat.title,
          model_id: this.item.id,
        })
        this.$store.commit('SETSHAREID', temp.id)
        this.$store.commit('SETSHARENAMENAME', chat.title)
        this.$store.commit('myAddHistoryItem', chat)
        this.$store.commit('SAVECHATLIST', {
          role: 'assistant',
          content: chat.desc,
          status: 'false',
          tepId: uni.$u.guid(),
          avatar: this.item.avatar_url,
        })
        this.$store.commit('setShowHistory', false)

        // 这里也要新建
        uni.redirectTo({
          url: '/pages/home/home',
        })
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.newchat {
  background-color: rgb(246, 247, 249);
  .title {
    text-align: center;
    background-color: rgb(246, 247, 249);
    padding: 48rpx 0;
    font-size: 28rpx;
  }
  .btn {
    width: 366rpx;
    margin: 56rpx auto;
  }
}
::v-deep .u-button__text {
  font-size: 32rpx !important;
}
::v-deep .u-checkbox-group {
  text-align: center;
  justify-content: center;
  margin-top: 46rpx;
}
</style>
